<template>
  <div id="main">
    <div id="left">
      <el-form ref="form" :model="form" label-width="100px">
      <!-- <el-button type="primary" :icon="Edit"></el-button> -->

      <el-form-item align = "left" label="网络">
      <el-select
        style="margin-left: 20px"
        size="mini"
        v-model="value"
        disabled
      >
        <el-option
          size="mini"
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <el-button
        v-on:click="connectWifi"
        id="setWifi"
        type="success"
        v-bind:loading="loading"
        v-if="isAble"
        size="mini"
        style="margin-left: 20px"
        round
        disabled
        >确认修改</el-button>
      </el-form-item>
      <el-row>
      <el-form-item label="自启" align = "left" inline= true>
  <el-switch v-model="onOffAble" v-on:click="setAutoStart"  inline-prompt active-text="是" inactive-text="否">
  </el-switch>
      </el-form-item>

            <el-form-item label="有线连接 " align = "left" inline= true>
  <el-switch v-model="isWifi" v-on:click="setIsWifi" disabled  inline-prompt active-text="是" inactive-text="否">
  </el-switch>
      </el-form-item>
      </el-row>
  </el-form>


      <el-descriptions
        align = "center"
        :column="1"
        size="mini"
        border
        id="info"
      >
        <el-descriptions-item  size = "mini" label="开发者" align = "center">杨康</el-descriptions-item>
        <el-descriptions-item  label="版本" align = "center">v1.0</el-descriptions-item>
        <el-descriptions-item  label="仓库地址" align = "center">https://gitee.com/y_kang/isip-net-connect</el-descriptions-item>
        <el-descriptions-item  label="说明" align = "center">ISIP实验室(529)联网程序功能：<br>
          1. 自动联网
          2. 检测网络状态，断网重连
        </el-descriptions-item>
      </el-descriptions>
      
    </div>
    <div id="right"></div>
  </div>
</template>

<script>
import "../components/renderer.js";
import { ref } from "vue";
import axios from 'axios'
const { ipcRenderer } = require("electron");
const Store = require('electron-store');


const store = new Store();
// store.set('unicorn', 'unicorn');
// console.log(store.get('unicorn'));
export default {
  name: "App",
  components: {},
  data() {
    return {
      options: ref([
        {
          value: "ISIP_5G",
          label: "ISIP_5G",
        },
        {
          value: "ISIP_2.4G",
          label: "ISIP_2.4G",
        },
      ]),
      value: ref(""),
      wifiName: null,
      isAble: true,
      loading: false,
      onOffAble: true,
      isWifi:true,
      isSuccess:false
    };
  },
  created() {
    // this.getWifiConfig();
    // this.init()
    var t = store.get('onOffAble')
    if(t != undefined){
      this.onOffAble = t
      this.setAutoStart(this.onOffAble)
    }
  },
  methods: {
    getWifiConfig() {

    },
    connectWifi(){
      store.set('wifi', this.value);
      this.wifiName = this.value
      // 调用cmd连接wifi
      // ipcRenderer.send("send-message-to-main",this.wifiName);
      this.loading = true;
      this.isAble = false;
    },
    connectResult(result){
      if(result.indexOf("ok")){
        this.loading = false;
        this.isAble = true;
      }
    },
    init(){
      var wifiName = store.get('wifi');
      console.log("初始化得到的名字  :" + wifiName)
      // 先判断配置文件里面是否为空，为空则表示是一台新的电脑，默认连接 5G
      if(wifiName == undefined) store.set('wifi', 'ISIP_5G');
      this.wifiName = wifiName;

      var isWifiFlag = store.get("isWifi");
      if(isWifiFlag == undefined) store.set('isWifi', true);
      this.isWifi = true
      ipcRenderer.on("send-message-to-cli",(event, args)=>{
        console.log("渲染进程收到数据："+args)
        if(args.indexOf("post")){
          this.sendPost()
        }
      })
    },
    setAutoStart(){
      console.log("设置开机自启，状态为："+ this.onOffAble)
      ipcRenderer.send('openAutoStart',this.onOffAble)
      store.set('onOffAble', this.onOffAble);
    },
    setIsWifi(){
      store.set('wifi', this.isWifi);
    },
    sendPost(){
      axios({
        method:'post',
        url:'http://172.18.0.3/a70.htm',
        headers:{
          "Host":"172.18.0.3",
          "Content-Type":"application/x-www-form-urlencoded",
          "Origin": "http://172.18.0.3",
          "Connection":"keep-alive"
        },
        data:"DDDDD=dk431&upass=dianke&R1=0&R2=&R6=0&para=00&0MKKey=123456&buttonClicked=&redirect_url=&err_flag=&username=&password=&user=&cmd=&Login="
      }).then(function(response){
        console.log(response)
      }, function(response){
        console.log(response)
      });
    },

  
},
};





</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
#info {
  /* margin-left: 10%; */
}
</style>
